package com.crpc.core.serialize;

import com.crpc.core.config.ObjectWrapper;
import com.crpc.core.serialize.impl.HessianSerializer;
import com.crpc.core.serialize.impl.JDKSerializer;
import com.crpc.core.serialize.impl.JSONSerializer;
import com.crpc.core.serialize.impl.ProtoBufSerializer;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import static com.crpc.common.crpcEnum.SerializerEnums.*;

/**
 * @author wzy
 * @version 1.0
 * @description:
 * @date 2023/9/29 15:38
 */
@Slf4j
public class SerializerFactory {
    private static final ConcurrentHashMap<String, ObjectWrapper<SerializerWrapper>> SERIALIZER_CACHE = new ConcurrentHashMap<>();
    private static final ConcurrentHashMap<Byte, ObjectWrapper<SerializerWrapper>> SERIALIZER_CACHE_CODE = new ConcurrentHashMap<>();

    static {
        // 创建Hessian序列化器
        SerializerWrapper hessian = new SerializerWrapper(HESSIAN.getCode(), HESSIAN.getType(), new HessianSerializer());
        // 创建JSON序列化器
        SerializerWrapper json = new SerializerWrapper(JSON.getCode(), JSON.getType(), new JSONSerializer());
        // 创建JDK序列化器
        SerializerWrapper jdk = new SerializerWrapper(JDK.getCode(), JDK.getType(), new JDKSerializer());
        // 创建Protobuf序列化器
        SerializerWrapper protobuf = new SerializerWrapper(PROTOBUF.getCode(), PROTOBUF.getType(), new ProtoBufSerializer());
        // 将Hessian序列化器放入缓存
        ObjectWrapper<SerializerWrapper> hessianObject = new ObjectWrapper<>(HESSIAN.getCode(), HESSIAN.getType(), hessian);
        // 将JSON序列化器放入缓存
        ObjectWrapper<SerializerWrapper> jsonObject = new ObjectWrapper<>(JSON.getCode(), JSON.getType(), json);
        // 将JDK序列化器放入缓存
        ObjectWrapper<SerializerWrapper> jdkObject = new ObjectWrapper<>(JDK.getCode(), JDK.getType(), jdk);
        // 将Protobuf序列化器放入缓存
        ObjectWrapper<SerializerWrapper> protobufObject = new ObjectWrapper<>(PROTOBUF.getCode(), PROTOBUF.getType(), protobuf);
        // 将Hessian序列化器放入缓存
        SERIALIZER_CACHE.put(HESSIAN.getType(), hessianObject);
        // 将JSON序列化器放入缓存
        SERIALIZER_CACHE.put(JSON.getType(), jsonObject);
        // 将JDK序列化器放入缓存
        SERIALIZER_CACHE.put(JDK.getType(), jdkObject);
        // 将Protobuf序列化器放入缓存
        SERIALIZER_CACHE.put(PROTOBUF.getType(), protobufObject);
        // 将Hessian序列化器放入缓存
        SERIALIZER_CACHE_CODE.put(HESSIAN.getCode(), hessianObject);
        // 将JSON序列化器放入缓存
        SERIALIZER_CACHE_CODE.put(JSON.getCode(), jsonObject);
        // 将JDK序列化器放入缓存
        SERIALIZER_CACHE_CODE.put(JDK.getCode(), jdkObject);
        // 将Protobuf序列化器放入缓存
        SERIALIZER_CACHE_CODE.put(PROTOBUF.getCode(), protobufObject);
    }

    public static SerializerWrapper getSerializer(String serializeType) {
        ObjectWrapper<SerializerWrapper> objectWrapper = SERIALIZER_CACHE.get(serializeType);
        return objectWrapper != null ? objectWrapper.getObjectImpl() : SERIALIZER_CACHE.get(HESSIAN.getType()).getObjectImpl();
    }

    public static SerializerWrapper getSerializer(byte serializeCode) {
        ObjectWrapper<SerializerWrapper> objectWrapper = SERIALIZER_CACHE_CODE.get(serializeCode);
        return objectWrapper != null ? objectWrapper.getObjectImpl() : SERIALIZER_CACHE_CODE.get(HESSIAN.getCode()).getObjectImpl();
    }

    public static void addSerializerList(List<ObjectWrapper<Serializer>> objectWrapper) {
        for (ObjectWrapper<Serializer> wrapper : objectWrapper) {
            ObjectWrapper<SerializerWrapper> serializerWrapperObjectWrapper = new ObjectWrapper<>(wrapper.getObjectCode(), wrapper.getObjectType(), new SerializerWrapper(wrapper.getObjectCode(), wrapper.getObjectType(), wrapper.getObjectImpl()));
            SERIALIZER_CACHE.put(wrapper.getObjectType(), serializerWrapperObjectWrapper);
            SERIALIZER_CACHE_CODE.put(wrapper.getObjectCode(), serializerWrapperObjectWrapper);
        }
    }
}
